﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace MTGTools
{
    class GUICacheManager
    {
        #region variables

        static LinkedList<GUICachedImage> imageCache = new LinkedList<GUICachedImage>();
        static Image unavailableImage = null;

        #endregion

        #region properties

        static public LinkedList<GUICachedImage> ImageCache
        {
            get { return imageCache; }
            set { imageCache = value; }
        }

        #endregion

        #region methods

        public static Image getImage(string name)
        {
            //check if image has been used before.
            foreach(GUICachedImage image in imageCache)
                if(image.Name.ToLower().Equals(name.ToLower()))
                    return image.Picture;

            string possibleFilename = "images\\" + name + ".jpg";

            if (File.Exists(possibleFilename))
            {
                addImage(possibleFilename, name);
                    return getImage(name);
            }

            return unavailableImage;
        }

        public static void addImage(string filename, string name)
        {
            if (File.Exists(filename))
            {
                Image image = new System.Drawing.Bitmap(filename);
                image.Tag = "";
                imageCache.AddLast(new GUICachedImage(image, name));
            }
            else
                imageCache.AddLast(new GUICachedImage(unavailableImage, name));
        }

        public static void setNullImage(Image nullImage)
        {
            unavailableImage = nullImage;
            unavailableImage.Tag = "";
        }

        #endregion
    }
}
